package api

import (
	. "com.mgface.disobj/common"
	"fmt"
	log "github.com/sirupsen/logrus"
	"net"
	"time"
)

func StartApiHeartbeat(metanodeAddr , nodeAddr string) {
	mnAddr = metanodeAddr //把MQ的地址写入通道
	client := NewReconTCPClient(metanodeAddr)
	cmd := &Cmd{Name: "set", Key: "apiNodes", Value: nodeAddr}
start:
	for {
		cmd.Run(client)
		//判断执行的错误异常
		if cmd.Error != nil {
			t, ok := cmd.Error.(*net.OpError)
			if ok {
				if t.Op == "write" {
					//重连
					log.Debug(fmt.Sprintf("[心跳服务]等待重连元数据服务端[%s]...", metanodeAddr))
				reconn:
					client = NewReconTCPClient(metanodeAddr)
					if client == nil {
						log.Debug(fmt.Sprintf("[心跳服务]连接元数据服务%s异常,请检查其状态.", metanodeAddr))
						goto reconn
					} else {
						log.Debug("[心跳服务]重连元数据服务端成功.")
						goto start
					}
				}
			}
		} else {
			//执行成功，则暂停3秒
			time.Sleep(time.Second * 3)
		}
	}
}
